package P160;

/**
 * @Author DJ同学
 * @Date 2021/2/25 16:50
 * @Version 1.0
 * @Name 相交链表
 * @Problem https://leetcode-cn.com/problems/intersection-of-two-linked-lists/
 * @Idea 让两个指针走同样的路(三份路)，之后看他们会不会相遇
 * 参考：https://leetcode-cn.com/problems/intersection-of-two-linked-lists/solution/jiao-ni-yong-lang-man-de-fang-shi-zhao-dao-liang-2/
 */
public class Solution {

    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if(headA==null)return null;
        if(headB==null)return null;
        ListNode a = headA;
        ListNode b = headB;
        int cnta=0;
        int cntb=0;
        while(cnta<2&&cntb<2){
            if(a==null){
                a=headB;
                cnta++;
            }
            if(b==null){
                b=headA;
                cntb++;
            }
            if(a!=null&&a==b){
                return a;
            }
            a=a.next;
            b=b.next;
        }
        return null;
    }

}
